import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:thingskit/components/inputBox.dart';
import '../../../components/selectBox.dart';
import '../../../utils/const.dart';

enum LightModeEnum { auto, manual }

class LightCollapseController extends GetxController {
  final mode = LightModeEnum.auto.obs;
  final fixedBrightness = TextEditingController(text: '0'); //固定亮度
  final lightDelayTime = TextEditingController(text: '5'); //亮灯延迟事件
  final peopleBrightness = TextEditingController(text: '0'); // 有人亮度
  final noPeopleBrightness = TextEditingController(text: '0'); // 无人亮度
  final darkDelayTime = TextEditingController(text: '5'); // 灯灭延迟时间

  void setDefault() {
    mode.value = LightModeEnum.auto;
    fixedBrightness.text = '0';
    lightDelayTime.text = '5';
    peopleBrightness.text = '0';
    noPeopleBrightness.text = '0';
    darkDelayTime.text = '5';
  }

  void setModeFromJson(Map<String, dynamic> json) {
    mode.value = json['mode'] == 0 ? LightModeEnum.auto : LightModeEnum.manual;
    fixedBrightness.text = json['fixedBrightness'].toString();
    lightDelayTime.text = json['lightDelayTime'].toString();
    peopleBrightness.text = json['peopleBrightness'].toString();
    noPeopleBrightness.text = json['unmannedBrightness'].toString();
    darkDelayTime.text = json['dark_delay'].toString();
  }
}

class LightCollapse extends StatelessWidget {
  LightCollapse({super.key});

  final LightCollapseController controller = Get.put(LightCollapseController());

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
      child: Column(
        children: [
          Row(
            children: [
              const SizedBox(
                width: 40,
                child: Text('模式', style: TextStyle(fontSize: 18)),
              ),
              const SizedBox(width: 10),
              Obx(
                () => SegmentedButton(
                  segments: const <ButtonSegment<LightModeEnum>>[
                    ButtonSegment<LightModeEnum>(
                      value: LightModeEnum.auto,
                      label: Text('自动', style: TextStyle(fontSize: 14)),
                    ),
                    ButtonSegment<LightModeEnum>(
                      value: LightModeEnum.manual,
                      label: Text('手动', style: TextStyle(fontSize: 14)),
                    ),
                  ],
                  selected: {controller.mode.value},
                  onSelectionChanged: (value) {
                    controller.mode.value = value.first;
                  },
                  style: ButtonStyle(
                    visualDensity: VisualDensity.compact,
                    padding: WidgetStateProperty.all(
                      EdgeInsets.symmetric(horizontal: 12, vertical: 0),
                    ),
                    backgroundColor: WidgetStateProperty.resolveWith<Color>((
                      Set<WidgetState> states,
                    ) {
                      if (states.contains(WidgetState.selected)) {
                        return Colors.blue;
                      }
                      return Colors.transparent;
                    }),
                    foregroundColor: WidgetStateProperty.resolveWith<Color>((
                      Set<WidgetState> states,
                    ) {
                      if (states.contains(WidgetState.selected)) {
                        return Colors.white;
                      }
                      return Colors.black;
                    }),
                    iconColor: WidgetStateProperty.resolveWith<Color>((
                      Set<WidgetState> states,
                    ) {
                      if (states.contains(WidgetState.selected)) {
                        return Colors.white;
                      }
                      return Colors.black;
                    }),
                    shape: WidgetStateProperty.all<OutlinedBorder>(
                      RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(4),
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
          const SizedBox(height: 10),
          Obx(
            () =>
                controller.mode.value == LightModeEnum.auto
                    ? _buildAutoMode()
                    : _buildManualMode(),
          ),
        ],
      ),
    );
  }

  Widget _buildAutoMode() {
    return Column(
      children: [
        _buildPickerRow(
          '有人亮度',
          controller.peopleBrightness,
          '%',
          brightnessOptions,
        ),
        const SizedBox(height: 10),
        _buildInputRow('休眠延时', controller.lightDelayTime, '秒'),
        const SizedBox(height: 10),
        _buildPickerRow(
          '无人亮度',
          controller.noPeopleBrightness,
          '%',
          brightnessOptions,
        ),
        const SizedBox(height: 10),
        _buildInputRow('关灯延时', controller.darkDelayTime, '秒'),
      ],
    );
  }

  Widget _buildManualMode() {
    return _buildPickerRow(
      '固定亮度',
      controller.fixedBrightness,
      '%',
      brightnessOptions,
    );
  }

  Widget _buildInputRow(
    String label,
    TextEditingController controller,
    String suffix,
  ) {
    return Row(
      children: [
        SizedBox(width: 40, child: Text(label, style: TextStyle(fontSize: 18))),
        SizedBox(width: 10),
        Expanded(
          child:InputBox(controller: controller)
        ),
        SizedBox(width: 10),
        SizedBox(
          width: 20,
          child: Text(suffix, style: TextStyle(fontSize: 20)),
        ),
      ],
    );
  }

  Widget _buildPickerRow(
    String label,
    TextEditingController controller,
    String suffix,
    List<String> options,
  ) {
    return Row(
      children: [
        SizedBox(width: 40, child: Text(label, style: TextStyle(fontSize: 18))),
        SizedBox(width: 10),
        SelectBox(
          initialValue: controller.text,
          options: options,
          onChanged: (value) {
            controller.text = value;
          },
          label: '',
        ),
        SizedBox(width: 10),
        SizedBox(
          width: 20,
          child: Text(suffix, style: TextStyle(fontSize: 20)),
        ),
      ],
    );
  }
}
